var addEigenValueDialogIndex = null;
var alarmSettingDialogIndex = null;
var allData = [];
var selectedData = [];
var table = null;

var showEigenValueDialog = function () {
    refreshAllTable();
    addEigenValueDialogIndex = layer.open({
        type: 1,
        title: "选择特征值",
        shadeClose: true,
        btn: ['确定', '关闭'],
        skin: 'layui-layer-lan',
        shade: 0.8,
        area: ['700px', '450px'],
        content: $("#eigenValueAll"),
        yes: function (index, layero) {
            addSelectedEigenValues();
            layer.close(index);
        }
    });
}

var showAlarmSettingDialog = function (data) {
    loadAlarmSetting(data);
    alarmSettingDialogIndex = layer.open({
        type: 1,
        title: "设置报警值",
        shadeClose: true,
        btn: ['确定', '关闭'],
        skin: 'layui-layer-lan',
        shade: 0.8,
        area: ['700px', '520px'],
        content: $("#alarmSetting"),
        yes: function (index, layero) {
            var myData = $("#detailform").serializeObject();
            data.baseline = myData.baseline;
            data.alarmType = myData.alarmType;
            if (parseInt(data.alarmType) < 4) {
                data.warning1 = myData.warning;
                data.danger1 = myData.danger;
            }
            else {
                data.warning1 = myData.warning1;
                data.warning2 = myData.warning2;
                data.danger1 = myData.danger1;
                data.danger2 = myData.danger2;
            }

            for (var o in selectedData) {
                if (selectedData[o].code == data.code) {
                    selectedData[o] = data;
                    break;
                }
            }

            table.reload("eigenValueTable", {data: selectedData});
            layer.close(index);
        }
    });
}

var addSelectedEigenValues = function () {
    var checkStatus = table.checkStatus("eigenValueAllTable");
    var cData = checkStatus.data;
    for (var i in cData) {
        var d = cData[i];
        selectedData.push(d);
    }
    table.reload("eigenValueTable", {data: selectedData});
}

var deleteSelectedEigenValues = function () {
    var checkStatus = table.checkStatus("eigenValueTable");
    var cData = checkStatus.data;
    if (cData.length < 1) {
        layer.msg("您没有选中记录");
        return;
    }
    layer.confirm('确认删除记录吗？', {
        btn: ['确认', '取消'],
        yes: function (index) {
            try {
                layer.close(index);
                var newSelectedData = new Array();
                for (var i in selectedData) {
                    var d = selectedData[i];
                    if (!isDataExists(cData, d)) {
                        newSelectedData.push(d);
                    }
                }
                selectedData = newSelectedData;
                table.reload("eigenValueTable", {data: newSelectedData});
            }
            catch (e) {
            }
        }
    });
}

var refreshAllTable = function () {
    var refreshData = new Array();
    $.each(allData, function (i, d) {
        if (!isDataExists(selectedData, d)) {
            refreshData.push(d);
        }
    });
    table.reload("eigenValueAllTable", {data: refreshData});
}

var isDataExists = function (checkedData, d) {
    for (var i in checkedData) {
        var m = checkedData[i];
        if (m.code == d.code) {
            return true;
        }
    }
    return false;
}


var loadAlarmTypes = function (category) {
    $(".alarm_div").hide();
    if (parseInt(category) > 1) {
        $(".alarm_" + category).show();
    }
}

$('#eigen_item_alarmType').on('change', function () {
    var val = $(this).val();
    loadAlarmTypes(val);
});

var loadAlarmSetting = function (data) {
    $(".alarm-control").val("");
    $(".alarm-select").val("");
    if (data) {
        if (data.alarmType) {
            $("#eigen_item_baseline").val(data.baseline);
            if (data.alarmType > 3) {
                $("#eigen_item_warning1").val(data.warning1);
                $("#eigen_item_warning2").val(data.warning2);
                $("#eigen_item_danger1").val(data.danger1);
                $("#eigen_item_danger2").val(data.danger2);
            }
            else {
                $("#eigen_item_warning").val(data.warning1);
                $("#eigen_item_danger").val(data.danger1);
            }
            $("#eigen_item_alarmType").val(data.alarmType);
            $("#eigen_item_alarmType").trigger("change");
        }
        else {
            $("#eigen_item_alarmType").val(1);
            $("#eigen_item_alarmType").trigger("change");
        }
    }
    else {
        $("#eigen_item_alarmType").val(1);
        $("#eigen_item_alarmType").trigger("change");
    }
}


var initTable = function (t, d, sd) {
    allData = d;
    selectedData = sd;
    table = t;

    table.render({
        elem: '#eigenValueTable'
        , id: 'eigenValueTable'
        , cols: [
            [
                {type: 'checkbox'},
                {field: 'name', title: '名称', sort: true},
                {field: 'code', width: '8%', title: '编码', sort: true},
                {field: 'unit', width: '8%', title: '单位', sort: true},
                {field: 'dataSource', width: '8%', title: '数据来源', sort: true},
                {field: 'baseline', width: '8%', title: '基准值', sort: true},
                {field: 'alarmType', width: '8%', title: '报警方式', sort: true},
                {field: 'warning1', width: '8%', title: '预警值1', sort: true},
                {field: 'warning2', width: '8%', title: '预警值2', sort: true},
                {field: 'danger1', width: '8%', title: '危险值1', sort: true},
                {field: 'danger2', width: '8%', title: '危险值2', sort: true},
                {fixed: 'right', width: '8%', align: 'left', toolbar: '#eigenBar'}
            ]
        ]
        , size: 'sm'
        , data: selectedData
        , even: true
    });

    table.on('tool(eigenValueTable)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if (layEvent === 'edit') {
            showAlarmSettingDialog(data);
        }
    });


    table.render({
        elem: '#eigenValueAllTable'
        , id: 'eigenValueAllTable'
        , cols: [
            [
                {type: 'checkbox'},
                {field: 'name', title: '名称', sort: true},
                {field: 'code', width: '20%', title: '编码', sort: true},
                {field: 'unit', width: '15%', title: '单位', sort: true},
                {field: 'dataSource', width: '15%', title: '数据来源', sort: true},
            ]
        ]
        , data: []
        , even: true
        , size: 'sm'
        , page: true
        , height: 250
        , limit: 5
        , limits: [5, 10, 15]
    });
}